1 : <?php
2 : /*--------------------------------------------------------------------------+
3 : This file is part of eStudy.
4 : common/classes/class.formular.inc.php
5 : - Modulgruppe: Framework
6 : - Beschreibung: Klasse zum Erstellen eines Formulars.
7 : - Version: 0.3, 11/06/06
8 : - Autor(en): Thorsten Buss <admin@drstone.de>
9 : +---------------------------------------------------------------------------+
10 : This program is free software; you can redistribute it and/or
11 : modify it under the terms of the GNU General Public License
12 : as published by the Free Software Foundation; either version 2
13 : of the License, or any later version.
14 : +---------------------------------------------------------------------------+
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 : +--------------------------------------------------------------------------*/
23 : /**
24 : * Klasse zum erstellen eines Formulars
25 : *
26 : * Mit dieser Klasse kann man aus PHP schoen und Sauber ein Formular erstellen,
27 : * ohne auf die Individualitaet verzichten zu muessen.
28 : * @author Thorsten Buss <admin@drstone.de>
29 : * @version 0.3, 11/06/06
30 : */
31 : /**
32 : * Klasse zum erstellen eines Formulars
33 : *
34 : * @access public
35 : */
36 : class Formular {
37 : /**
38 : * @var string Forumlar Kopf
39 : * @access private
40 : */
41 : var $formHead;
42 : /**
43 : * Sende-Methode des Formulars: get oder post
44 : * @var string
45 : * @access private
46 : */
47 : var $method;
48 : /**
49 : * @var string Tag der zum naechsten Element hinzugefuegt wird
50 : * @access private
51 : */
52 : var $addToNext;
53 : /**
54 : * @var string Forumlar Inhalt (Die Komplette Tabelle mit InputElementen)
55 : * @access private
56 : */
57 : var $formString;
58 : /**
59 : * @var string TabellenTemplate (Kopf und Fuss der Tabelle mit ###CONTENT### getrennt)
60 : * @access private
61 : */
62 : var $tableTemplate;
63 : /**
64 : * @var string Linien/TabellenZeilen Template (Anfang und Ende der Zeile mit ###TEXT### und ###INPUT### dazwischen)
65 : * @access private
66 : */
67 : var $lineTemplate;
68 : /**
69 : * @var string Code fuer die Buttons des Formulars
70 : * @access private
71 : */
72 : var $buttons;
73 : /**
74 : * @var string error Code der Ausgegeben wird
75 : * @access private
76 : */
77 : var $ERROR;
78 : /**
79 : * @var int Anzahl der gesetzten Marker
80 : * @access private
81 : */
82 : var $markerCount;
83 : /**
84 : * @var string Code für hidden fields
85 : * @access private
86 : */
87 : var $hiddenFields;
88 : /**
89 : * Konstruktor
90 : *
91 : * @param string [$name] Siehe setHead()
92 : * @param string [$action] Siehe setHead()
93 : * @param string [$method] Siehe setHead()
94 : * @access public
95 : */
96 : function Formular($name = '', $action = '', $method = "post") {
97 0 : $this->clear();
98 0 : $this->setFormHead($name, $action, $method);
99 0 : }
100 : /**
101 : * Setzt alle Einstellungen zurueck
102 : *
103 : * @access public
104 : */
105 : function clear() {
106 0 : $this->formString = "";
107 0 : $this->markerCount = 0;
108 0 : $this->setButtons(1);
109 0 : $this->setFormHead();
110 0 : $this->hiddenFields = "";
111 0 : }
112 : /**
113 : * Setzt diverse Eigenschaften des FORM Elements
114 : *
115 : * @param string [$name] Name des FORM Elements
116 : * @param string [$action] Action bei Klicken des submit buttons
117 : * @param string [$method] Methode: post|get
118 : * @param string [$enctype] Enctype Eigenschaft des FORM Elements
119 : * @access public
120 : */
121 : function setFormHead($name = '', $action = '', $method = "post", $enctype = 'multipart/form-data', $addAttributes = "") {
122 0 : if ($action == '') $action = PATH_TO_ROOT.SCRIPT_NAME;
123 0 : if ($this->formString != "") {
124 0 : $this->ERROR = "Fehler: Erst setFormHead(), danach addLine()!<br />";
125 0 : return false;
126 : }
127 0 : if ($method != "post" && $method != "get") $method = "post";
128 0 : $this->method = $method;
129 0 : $this->formHead = "<form ".(!empty($name) ? "id='$name' " : "") ."action='$action' method='$method' enctype='$enctype' $addAttributes>\n";
130 0 : return true;
131 : }
132 : /**
133 : * Legt die Generellen Eigenschaften fuer die einzelnen Zeilen fest
134 : *
135 : * @param string [$tableBegin] Komplettes Table Tag mit beliebigen Eigenschaften (zb. class, id, width...)
136 : * @param string [$txtField] Erster Teil eines TR und TD Tags mit beliebigen Eigenschaften (zb. class, id, width...)
137 : * @param string [$txtFieldEnd] Ende des TD und TR Tags
138 : * @param string [$inputField] Zweiter Teil eines /TD und TD Tags mit beliebigen Eigenschaften (zb. class, id, width...)
139 : * @param string [$inputFieldEnd] Ende des TD und TR Tags
140 : * @param string [$tableEnd] Ende des TABLE Tags
141 : * @access public
142 : */
143 : function setTemplate($tableBegin, $txtField, $inputField, $txtFieldEnd = "</td>", $inputFieldEnd = '</td></tr>', $tableEnd = '</table>') {
144 0 : $this->tableTemplate = $tableBegin."\n###CONTENT###\n".$tableEnd."\n";
145 0 : $this->lineTemplate = $txtField."\n###TEXT###\n$txtFieldEnd\n".$inputField."\n###INPUT###".$inputFieldEnd."\n";
146 0 : return true;
147 : }
148 : /**
149 : * Legt fest welche Buttons angezeigt werden sollen
150 : *
151 : * Verschiedene Arten :
152 : * 1 => 3 Buttons, OK, Zuruecksetzen, Abbrechen
153 : * 2 => 2 Buttons, OK und Abbrechen
154 : * 3 => 2 Buttons, Ja und Nein
155 : * @param int [$type] legt die Anordnung der Buttons fest
156 : * @param string [$value1] Legt den Text des 1ten Buttons fest
157 : * @param string [$value2] Legt den Text des 2ten Buttons fest
158 : * @param string [$value3] Legt den Text des 3ten Buttons fest
159 : * @access public
160 : */
161 : function setButtons($type, $value1 = '', $value2 = '', $value3 = '') {
162 : switch ($type) {
163 0 : case 1:
164 0 : if ($value1 == "") $value1 = "Abschicken";
165 0 : if ($value2 == "") $value2 = "Zurücksetzen";
166 0 : if ($value3 == "") $value3 = "Abbrechen";
167 0 : $this->buttons = "\n<p class='pForm'><input type='submit' name='okButton' value='$value1' /> <input type='reset' name='resetButton' value='$value2' /> <input type='submit' name='cancelButton' value='$value3' /></p>";
168 0 : break;
169 :
170 0 : case 2:
171 0 : if ($value1 == "") $value1 = "Abschicken";
172 0 : if ($value2 == "") $value2 = "Abbrechen";
173 0 : $this->buttons = "\n<p class='pForm'><input type='submit' name='okButton' value='$value1' /> <input type='submit' name='cancelButton' value='$value2'/></p>";
174 0 : break;
175 :
176 0 : case 3:
177 0 : if ($value1 == "") $value1 = "Ja";
178 0 : if ($value2 == "") $value2 = "Nein";
179 0 : $this->buttons = "\n<p class='pForm'><input type='submit' name='jaButton' value='$value1' /> <input type='submit' name='neinButton' value='$value2'/></p>";
180 0 : break;
181 :
182 0 : default:
183 0 : break;
184 0 : }
185 0 : return true;
186 : }
187 : /**
188 : * Fuegt die Buttons an dieses Stelle falls sie Vorher festgelegt wurden.
189 : *
190 : * @access public
191 : */
192 : function addButtons() {
193 0 : $this->addRawCode("<tr><td colspan='2' align='center'><hr />".$this->buttons."</td></tr>");
194 0 : return true;
195 : }
196 : /**
197 : * Ueberprueft ob es einen $_POST wert fuer das feld $name gibt
198 : *
199 : * @param string [$name] Name des Feldes das ueberprueft werden soll
200 : * @param string [$value] Der Uebergebene Default Wert wenn es keine POST Variable gibt
201 : * @access private
202 : */
203 : function getValue($name, $value) {
204 0 : if ($this->method == "post" && isset($_POST[$name])) return Data::toHTML($_POST[$name]);
205 0 : elseif ($this->method == "get" && isset($_GET[$name])) return Data::toHTML($_GET[$name]);
206 0 : return Data::toHTML($value, false);
207 : }
208 : /**
209 : * Fuegt ein INPUT Tag hinzu
210 : *
211 : * @param string [$type] Wert fuer die type Eigenschaft des Tags
212 : * @param string [$name] Name der Name Eigenschaft des Tags
213 : * @param string [$description] Beschreibung fuer das TAG welche in der Linken TabellenSpalte angezeigt wird
214 : * @param int [$size] Groesse des Eingabefeldes
215 : * @param string [$value] Default Wert fuer das TAG
216 : * @param int [$maxlength] Wert des maxlength-Attributs ODER bei $type = checkbox Angabe, ob die Checkbox angehakt ist
217 : * @access public
218 : */
219 : function addInput($type, $name, $description, $size = 30, $value = "", $maxlength = 0) {
220 0 : $value = $this->getValue($name, $value);
221 0 : $HTMLmaxlength = $HTMLvalue = $HTMLsize = '';
222 0 : if ($maxlength != 0) $HTMLmaxlength = " maxlength='$maxlength'";
223 0 : if ($value != "") $HTMLvalue = " value=\"$value\"";
224 0 : if ($size != 30) $HTMLsize = " size='$size'";
225 0 : if ($type == "checkbox") {
226 0 : $HTMLmaxlength = '';
227 0 : $HTMLsize = '';
228 0 : $HTMLvalue = " value=\"$value\"".($maxlength > 0 || $this->getValue($name, "") != "" ? " checked='checked'" : "");
229 0 : }
230 0 : if ($type == 'hidden') $this->hiddenFields.= "<input type='hidden' name='$name'$HTMLvalue />\n";
231 0 : else $this->addLine($description, "<input type='$type' name='$name'$HTMLvalue$HTMLsize$HTMLmaxlength />\n");
232 0 : return true;
233 : }
234 : /**
235 : * Fuegt eine beliebige anzahl von RADIO Buttons hinzu
236 : *
237 : * @param string [$name] Name der Name Eigenschaft des Tags
238 : * @param array [$data] Zweidimensionales Array mit der Beschreibung[0] und den Werten der einzelnen Optionen[1]
239 : * @param string [$description] Beschreibung fuer das TAG welche in der Linken TabellenSpalte angezeigt wird
240 : * @param string [$checked] Enthaelt den Wert des Elements welches Defaultmaessig ausgewaehlt sein soll
241 : * @access public
242 : */
243 : function addRadio($name, $data, $description, $checked = '') {
244 0 : if (!is_array($data)) {
245 0 : $this->ERROR = "Fehler: Sie muessen ein Array an addRadio() uebergeben!<br/>";
246 0 : return false;
247 : }
248 0 : $COUNT = 0;
249 0 : $string = '';
250 0 : foreach($data as $line) {
251 0 : if (!isset($line[2])) $line[2] = '';
252 0 : $checkedAttr = "";
253 0 : if ($this->getValue($name, $checked) == $line[1]) $checkedAttr = "checked='checked'";
254 0 : $string.= "<input id='ID_{$name}_${line[1]}' type='radio' name='$name' value='${line[1]}' $checkedAttr $line[2] /><label for='ID_{$name}_${line[1]}'> ${line[0]}</label><br />\n";
255 0 : }
256 0 : $this->addLine($description, $string);
257 0 : return true;
258 : }
259 : /**
260 : * Fuegt ein TEXTAREA Tag hinzu
261 : *
262 : * @param string [$name] Name der Name Eigenschaft des Tags
263 : * @param string [$description] Beschreibung fuer das TAG welche in der Linken TabellenSpalte angezeigt wird
264 : * @param int [$cols] Breite des TEXTfeldes
265 : * @param int [$rows] Hoehe in Zeilen des TEXTFeldes
266 : * @param string [$value] Default Wert fuer das TAG
267 : * @access public
268 : */
269 : function addTextArea($name, $description, $cols, $rows, $value = "") {
270 0 : $value = $this->getValue($name, $value);
271 0 : $this->addLine($description, "<textarea name='$name' cols='$cols' rows='$rows'>$value</textarea>\n");
272 0 : return true;
273 : }
274 : /**
275 : * Fuegt ein SELECT Feld hinzu
276 : *
277 : * @param string [$name] Name der Name Eigenschaft des Tags
278 : * @param string [$description] Beschreibung fuer das TAG welche in der Linken TabellenSpalte angezeigt wird
279 : * @param array [$data] Eindimensionales Array mit den Werten er einzelnen Optionen [2Dimensional wenn Value mit angegeben] (0=>Angezeigt [1=>value])
280 : * @param int [$size] Hoehe in Zeilen der List (1 = DropDown)
281 : * @param bool [$multiple] Legt fest ob das Element Mehrfachauswahl zulaesst
282 : * @param string [$selected] Name oder Value des Eintrags welcher DEFAULT meassig ausgewaehl ist
283 : * @access public
284 : */
285 : function addSelect($name, $description, $data, $size = 1, $multiple = false, $selected = "") {
286 0 : if (!is_array($data)) {
287 0 : $this->ERROR = "Fehler: Sie muessen ein Array an addSelect() uebergeben!<br/>";
288 0 : return false;
289 : }
290 0 : if (isset($_POST[$name])) $selected = $_POST[$name];
291 0 : if ($multiple) {
292 0 : $name.= "[]";
293 0 : $multiple = "multiple='multiple' ";
294 0 : }
295 0 : $string = "<select name='$name' size='$size' $multiple>\n";
296 0 : foreach($data as $line) {
297 0 : $add = '';
298 0 : if (is_array($line)) {
299 0 : if (is_array($selected)) {
300 0 : foreach($selected as $select) if ($select == $line[1]) $add = 'selected="selected" ';
301 0 : }
302 0 : if ($selected == $line[0] || $selected == $line[1]) $add = 'selected="selected" ';
303 0 : $string.= "<option value='".$line[1]."' ".$add.">".$line[0]."</option>\n";
304 0 : } else {
305 0 : if (is_array($selected)) {
306 0 : foreach($selected as $select) if ($select == $line) $add = 'selected="selected" ';
307 0 : }
308 0 : if ($selected == $line) $add = 'selected="selected" ';
309 0 : $string.= "<option ".$add.">".$line."</option>\n";
310 : }
311 0 : }
312 0 : $string.= "</select>";
313 0 : $this->addLine($description, $string);
314 0 : return true;
315 : }
316 : /**
317 : * Fuegt eine beliebige Zeile in die Formular-Tabelle hinzu
318 : *
319 : * Moeglichkeit Benutzerdefinierte Tabellen zeilen einzufuegen
320 : * WICHTIG: Zeile muss folgenden Syntax haben:
321 : * <code><tr><td>xx</td><td>zz</td></tr></code>
322 : * @param string [$string] inhalt der Zeile
323 : * @access public
324 : */
325 : function addRawCode($string) {
326 0 : $this->formString.= $string."\n";
327 0 : if (isset($this->ERROR)) {
328 0 : $this->formString.= "<tr><td class='error' colspan='2'>".$this->ERROR."</td></tr>";
329 0 : unset($this->ERROR);
330 0 : }
331 0 : return true;
332 : }
333 : /**
334 : * Fuegt den Inhalt einer TabelleZeile mit Hilfe des LineTemplates ein
335 : *
336 : * @param string [$text] Text, der Links angezeigt wird
337 : * @param string [$input] Text, bzw. die INputElemente die Rechts angezeigt werden.
338 : * @access public
339 : */
340 : function addLine($text, $input) {
341 0 : $input = trim($input);
342 0 : if (isset($this->addToNext)) {
343 0 : $input = substr($input, 0, strpos($input, ' ')) .$this->addToNext.substr($input, strpos($input, ' '), strlen($input));
344 0 : unset($this->addToNext);
345 0 : }
346 0 : $string = str_replace("###TEXT###", $text, $this->lineTemplate);
347 0 : $string = str_replace("###INPUT###", "<p class='pForm'>".$input."</p>", $string);
348 0 : $this->addRawCode($string);
349 0 : return true;
350 : }
351 : /**
352 : * Fuegt dem naechsten Tag ein Atribut hinzu
353 : *
354 : * @param string [$string] der Tag der hinzugefuegt wird (bsp. style='width:10px' )
355 : * @access public
356 : */
357 : function addToNextTag($string) {
358 0 : $this->addToNext = " ".$string." ";
359 0 : return true;
360 : }
361 : /**
362 : * Fuegt einen Marker ein
363 : *
364 : * @access public
365 : */
366 : function addMarker() {
367 0 : $this->addRawCode("###MARKER###");
368 0 : $this->markerCount++;
369 0 : return true;
370 : }
371 : /**
372 : * Gibt den Code des Kompletten Formulars zurueck.
373 : *
374 : * Ohne Aufruf dieser Methode wird keine Ausgabe gemacht, es sei denn es gibt Fehler.
375 : * Mit hilfe von markern die man mit addMarker() setzen kann laesst sich die Ausgabe aufteilen
376 : * Zum beistpiel wenn man in das Formular etwas einfuegen moechte was die Klasse nicht unterstuetzt
377 : * Oder die Ausgabe einer anderen Funktion die nicht mit return sondern mit echo ihre ERgebniss zuruek gibt.
378 : * @param int [$marker] Gibt an welcher Teil des Dokuments ausgegeben werden soll.
379 : * @return string HTML-Formular
380 : * @access public
381 : */
382 : function getForm($marker = -1) {
383 0 : if ($marker >= 0 && $this->markerCount >= $marker) {
384 0 : $table = explode("###CONTENT###", $this->tableTemplate);
385 0 : $part = explode("###MARKER###", $this->formString);
386 0 : if ($marker == 0) { // Erste Ausgabe
387 0 : return "\n".$this->formHead.(!empty($this->hiddenFields) ? "<p class='pForm'>\n".$this->hiddenFields."</p>" : "") .$table[0].$part[0];
388 0 : } elseif ($marker == $this->markerCount) { // Letzte Ausgabe
389 0 : return $part[$marker].$table[1]."</form>\n";
390 : } else {
391 0 : return $part[$marker];
392 : }
393 : }
394 0 : return "\n".$this->formHead.(!empty($this->hiddenFields) ? "<p class='pForm'>\n".$this->hiddenFields."</p>" : "") .str_replace("###CONTENT###", $this->formString, $this->tableTemplate) ."</form>\n";
395 : }
396 : }
397 : /*
398 : BSP:
399 : $form->setFormHead("insert");
400 : $form->setTemplate("<table>","<tr><td class='tablecell'>","<td class='tablecell'>");
401 : $form->setButtons(1,"Abschicken");
402 :
403 : TrennZeile
404 : $form->addRawCode("<tr><TD class='tableHead' colspan='2'>Benötigte Einstellungen</TD></tr>");
405 :
406 : $form->addInput('text','test1','testestet');
407 : $form->addInput('password','test2','asgawertaw');
408 : $form->addInput('text','test3','asdfaser');
409 : $arr = array(0=>array('t1','2'),'t2','t3','t4');
410 :
411 : $form->addSelect('test55','SelectFeld',$arr,1);
412 :
413 : $RadioTest = array('t1','t2','t3','t4');
414 : $form->addRawCode("<tr><td colspan='2'>testLine</td></tr>");
415 : $form->addLine('beschreibung','TestFeld');
416 : $form->addRadio('testRadio',$RadioTest,'RadioTest');
417 : $form->addSelect('test6','SelectFeld',$arr,5, true);
418 :
419 : $form->addTextArea("txtTest","Testen",20,3,'asdfa');
420 : $form->addButtons();
421 : echo $form->getForm();
422 : */
|